package com.example.memosystem.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.memosystem.R;
import com.example.memosystem.activity.listen.ChangeListen;
import com.example.memosystem.activity.listen.TouchListen;
import com.example.memosystem.dao.impl.AdminDao;

public class ChangePwdActivity extends AppCompatActivity {

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_change_pwd);

        Intent intent = getIntent();
        String account = intent.getStringExtra("account");

        EditText one = findViewById(R.id.change_pwd_one);
        one.addTextChangedListener(new ChangeListen(one));
        EditText two = findViewById(R.id.change_pwd_two);
        two.addTextChangedListener(new ChangeListen(two));

        one.setOnTouchListener(new TouchListen(one));
        two.setOnTouchListener(new TouchListen(two));

        //初始状态
        one.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);
        two.setCompoundDrawablesWithIntrinsicBounds(0,0, 0, 0);

        Button btn = findViewById(R.id.change_pwd_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oneT = one.getText().toString().trim();
                String twoT = two.getText().toString().trim();
                if(oneT.isEmpty())
                {
                    Toast.makeText(ChangePwdActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
                }else if(twoT.isEmpty())
                {
                    Toast.makeText(ChangePwdActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }else if(!oneT.equals(twoT))
                {
                    Toast.makeText(ChangePwdActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                }else {
                    AdminDao adminDao = new AdminDao();
                    int a = adminDao.changePwdAdmin(account, oneT);
                    if(a == 0)
                    {
                        Toast.makeText(ChangePwdActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    }else if(a >= 1)
                    {
                        Toast.makeText(ChangePwdActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(ChangePwdActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        //返回按钮
        Toolbar toolbar = findViewById(R.id.toolbar_pwd);
        this.setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ChangePwdActivity.this, ManageActivity.class);
                intent1.putExtra("account", account);
                startActivity(intent1);
            }
        });

    }


}